package com.lingnan.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.common.utils.ResultEntity;
import com.lingnan.eduservice.entity.po.EduTeacherPO;
import com.lingnan.eduservice.entity.vo.EduTeacherLevelVO;
import com.lingnan.eduservice.entity.vo.EduTeacherQueryVO;
import com.lingnan.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzd
 * @Api : 开发文档注解
 * @RestController : ResponseBody + Controller
 * @RequestMapping : 请求路径 /eduservice/teacher
 * @CrossOrigin : 解决跨域问题
 * @since 2021-08-02
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    /**
     * 注入Service
     */
    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询所有教师功能
     *
     * @return
     */
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("findAllTeacher")
    public ResultEntity findAllTeacher() {

        // 查询所有教师信息
        List<EduTeacherPO> eduTeacherList = eduTeacherService.list(null);


        // 使用统一格式进行返回
        // 返回所有教师信息
        return ResultEntity.ok().data("EduTeacherList", eduTeacherList);
    }

    /**
     * 删除教师功能
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID逻辑删除讲师")
    @DeleteMapping("removeTeacherById/{id}")
    public ResultEntity removeTeacherById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id
    ) {
        System.out.println(id);
        // 删除教师信息
        boolean flag = eduTeacherService.removeById(id);

        // 返回操作信息
        if (flag) {
            // 返回删除成功信息
            return ResultEntity.ok();
        }
        // 返回删除失败信息
        return ResultEntity.error();
    }

    /**
     * 分页查询讲师信息
     *
     * @param current 当前页数
     * @param limit   每页显示条数
     * @return
     */
    @ApiOperation(value = "分页查询讲师信息")
    @GetMapping("pageTeacherList/{current}/{limit}")
    public ResultEntity pageTeacherList(
            @PathVariable("current") Long current,
            @PathVariable("limit") Long limit
    ) {
        // 创建Page对象
        Page<EduTeacherPO> eduTeacherList = new Page<>(current, limit);

        // 调用方法执行分页功能
        // 调用方法时,底层封装,把所有的分页数据封装到eduTeacherList中
        eduTeacherService.page(eduTeacherList, null);

        // 获取总记录数
        long total = eduTeacherList.getTotal();
        // 获取讲师数据
        List<EduTeacherPO> rows = eduTeacherList.getRecords();

        // 将数据存储到Map集合中
        Map map = new HashMap<>();
        map.put("total", total);
        map.put("rows", rows);

        // 返回数据
        return ResultEntity.ok().data(map);
    }

    /**
     * 根据条件查询教师
     *
     * @param current        当前页数
     * @param limit          每页显示条数
     * @param eduTeacherQueryVO 查询条件
     * @return
     */
    @ApiOperation(value = "根据条件查询教师")
    @PostMapping("pageTeacherQueryList/{current}/{limit}")
    public ResultEntity pageTeacherQueryList(
            @PathVariable("current") Long current,
            @PathVariable("limit") Long limit,
            @RequestBody(required = false) EduTeacherQueryVO eduTeacherQueryVO
    ) {
        // 创建分页查询对象
        Page<EduTeacherPO> eduTeacherList = new Page<>(current, limit);

        // 创建查询条件
        QueryWrapper<EduTeacherPO> wrapper = new QueryWrapper<>();

        // 多条件组合查询
        // 获取所有条件信息
        String name = eduTeacherQueryVO.getName();
        Integer level = eduTeacherQueryVO.getLevel();
        String begin = eduTeacherQueryVO.getBegin();
        String end = eduTeacherQueryVO.getEnd();

        // 判断条件是否为空,不为空则拼接当做条件
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }

        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }
        // 根据时间对结果进行排序
        wrapper.orderByDesc("gmt_create");

        // 执行查询
        eduTeacherService.page(eduTeacherList, wrapper);

        // 获取总记录数
        long total = eduTeacherList.getTotal();
        // 获取教师数据
        List<EduTeacherPO> rows = eduTeacherList.getRecords();

        Map map = new HashMap<>();
        map.put("total", total);
        map.put("rows", rows);

        // 返回查询数据
        return ResultEntity.ok().data(map);

    }

    /**
     * 添加教师功能
     *
     * @param eduTeacherPO
     * @return
     */
    @ApiOperation(value = "添加教师")
    @PostMapping("addTeacher")
    public ResultEntity addTeacher(@RequestBody EduTeacherPO eduTeacherPO) {
        // 添加教师信息
        boolean saveFlag = eduTeacherService.save(eduTeacherPO);
        // 返回添加响应
        if (saveFlag) {
            return ResultEntity.ok();
        }
        return ResultEntity.error();
    }

    /**
     * 根据id查询教师信息
     *
     * @param id 教师id
     * @return
     */
    @ApiOperation(value = "根据id查询教师信息")
    @GetMapping("findTeacherById/{id}")
    public ResultEntity findTeacherById(@PathVariable("id") String id) {

        // 根据id查询教师信息
        EduTeacherPO eduTeacherPO = eduTeacherService.getById(id);


        // 返回教师信息
        return ResultEntity.ok().data("eduTeacherPO", eduTeacherPO);

    }

    /**
     * 根据id修改教师信息
     *
     * @param eduTeacherPO 教师对象
     * @return
     */
    @ApiOperation(value = "根据id修改教师信息")
    @PostMapping("updateTeacherById")
    public ResultEntity updateTeacherById(@RequestBody EduTeacherPO eduTeacherPO) {
        // 根据id修改教师信息
        boolean updateFlag = eduTeacherService.updateById(eduTeacherPO);

        // 判断是否修改成功
        if (updateFlag) {
            return ResultEntity.ok();
        }
        return ResultEntity.error();
    }

    /**
     * 根据教师头衔分类(树形)
     * @return
     */
    @ApiOperation(value = "根据教师头衔分类(树形)")
    @GetMapping("findTeacherByLevel")
    public ResultEntity findTeacherByLevel(){
        // 执行查询
        List<EduTeacherLevelVO> eduTeacherLevelVOList = eduTeacherService.findTeacherByLevel();

        return ResultEntity.ok().data("eduTeacherLevelVOList",eduTeacherLevelVOList);
    }


}

